# == Schema Information
# Schema version: 36
#
# Table name: images
#
#  id      :integer       not null, primary key
#  name    :string(255)   not null
#  comment :string(255)   not null
#  author  :string(255)   not null
#

class Image < ActiveRecord::Base
  # Class variable to define the versions of every image that are going to be generated
  @@possible_sizes = { "thumb" => "90x90", "small"=> "400x400" , "medium" => "640x480>" }
  validates_length_of :author, :minimum => 3
  validates_length_of :comment, :minimum => 5
  validates_length_of :name, :minimum => 5
 
  file_column :name, :magick => { 
          :versions => @@possible_sizes
        }
  
  
  def self.find_by_comment(query)
    unless query=~/\A(\s*\w*\s*)*\Z/
          query = ""
    end
    # Extra paranoind gsub just in case 
     query = query.gsub(/;|,|'|\*|"|\)|\(/,'')
    self.find :all, :conditions => ["comment ~* ? ", query]
  end
  
  # Escaping html
  def before_save
    ERB::Util.h(self.name)
    ERB::Util.h(self.comment)
  end
  
  def self.possible_sizes
    return @@possible_sizes
  end
  
  
end
